package ru.zombator.taskman.dbadmin.actions;

import java.lang.reflect.InvocationTargetException;

import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.SubMonitor;
import org.eclipse.jface.operation.IRunnableWithProgress;

import ru.zombator.taskman.core.dbclient.DbException;
import ru.zombator.taskman.core.dbclient.IDbClient.Role;
import ru.zombator.taskman.core.domain.IPerson;
import ru.zombator.taskman.dbadmin.AdminApp;

/**
 * Задача "добавление пользователя"
 * @author nvamelichev
 *
 */
public class AddUserTask implements IRunnableWithProgress {
	private IPerson newPerson;
	
	private AdminApp mainApp;
	
	private String name;
	private String ipAddress;
	private Role role;
	
	public AddUserTask(AdminApp mainApp, String name, String ipAddress, Role role) {
		this.mainApp = mainApp;
		this.name = name;
		this.ipAddress = ipAddress;
		this.role = role;
	}
	
	public void run(IProgressMonitor pm) throws InvocationTargetException,
	InterruptedException {
		SubMonitor subPm = SubMonitor.convert(pm, IProgressMonitor.UNKNOWN);
		try {
			newPerson = mainApp.getClient().addUser(name, ipAddress, role);
		} catch (DbException dbe) {
			throw new InvocationTargetException(dbe);
		} finally {
			if (subPm != null)
				subPm.done();
		}
	}
	
	public IPerson getNewPerson() {
		return newPerson;
	}
}
